package com.qk.controller;

import com.qk.anno.Log;
import com.qk.common.Result;
import com.qk.entity.Dept;
import com.qk.common.PageResult;
import com.qk.server.DeptServer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@RequestMapping("/depts")
@RestController
public class DeptController {
    @Autowired
    private DeptServer deptServer;
    /**
     * 添加数据
     * */
    @Log
    @PostMapping
    public Result addDept(@RequestBody Dept dept){
        dept.setUpdateTime(LocalDateTime.now());
        deptServer.addDept(dept);
        log.info("添加部门成功{}",dept);
        return Result.success();
    }
    /**
     * 查询部门列表
     * */
    @GetMapping
    public Result findDeptByList(@RequestParam(value = "name",required = false) String  name, Integer status,
                                 @RequestParam(defaultValue = "1") Integer page,@RequestParam(defaultValue = "10") Integer pageSize){
        PageResult<Dept> pageResult = deptServer.findDeptByList(name,status,page,pageSize);
        log.info("查询部门列表成功{}",pageResult);
        return Result.success(pageResult);
    }
    /**
     * 根据id查询部门
     * */

    @GetMapping("/{id}")
    public Result findDeptById(@PathVariable Integer id){
        Dept dept = deptServer.findDeptById(id);
        log.info("查询部门成功{}",dept);
        return Result.success(dept);
    }
    /**
     * 修改部门
     * */
    @Log
    @PutMapping
    public Result updateDept(@RequestBody Dept dept){
        deptServer.updateById(dept);
        log.info("修改部门成功{}",dept);
        return Result.success();
    }
    /**
     * 删除部门
     * */
    @Log
    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable Integer id){
        deptServer.deleteById(id);
        log.info("删除部门成功{}",id);
        return Result.success();
    }
    /**
     * 查询所有部门
     * */
    @GetMapping("/list")
    public Result findAll(){
        List<Dept> list =deptServer.findAll();
        log.info("查询所有部门:{}",list);
        return Result.success(list);
    }
}
